/* Copyright (C) 2011. ChengDu Unicon system, All Rights Reserved */

/*""FILE COMMENT""******************************* Technical reference data ****
* File Name	: crank.c
* Version 	: 1.00
* Device 	: SH72543
* Tool Chain 	: 
* H/W Platform	: 
* Description 	: 
******************************************************************************/

/******************************************************************************
* History 	: 09.08.2011 Ver. 1.00 First Release
*		: 
*FILE COMMENT END*********************************************************/

#include <include.h>

/**************************************************************************
 *Function   : CrankInit       
 *Description: Init the module as crank measure, a part of timer A and timer B                                                    
 *Input      :                              
 *Output     :                              
 *Return     : 
 *G Variable :                              
 *Notes      :                                   
 **************************************************************************/
void CrankInit( const ST_CRANK_HW_SET *const crankHWInitVal )
{
  /* Crank IO Init */
  PORTA.DR.BIT.DR10   = 1; //
  PORTA.IOR.BIT.IOR10 = 1; //The PA10 is set to output
  PORTA.DR.BIT.DR6    = 1; //Disable pullup of sensor input 
  PORTA.IOR.BIT.IOR6  = 1; //The PA6 is set to output
  PORTE.CR2.BIT.MD10  = 1; //PE10 is set to TOE10 output
  PORTE.IR.BIT.IR10   = 0; //Not inverted
  PORTE.CR1.BIT.MD0   = 1; //PE0 is set to PIA00 input
  
  /* Init TOE10, Be used to set offset value*/
  ATUE.TSTRE.BIT.STRE1   = 1; //Timer Start Register E1
  /* Timer Control Register, Select the counter clock of prescaler E from clock-bus lines 0 to 5 */
  ATUE.SUBBLOCK[1].TCRE.BIT.CKSELE  = 0; /* Clock-bus line 0 */
  /* Timer Output Control Register, 0:Signal is output as is on PWM output pin
   *                                1:Inverted signal is output on PWM output pin */
  ATUE.SUBBLOCK[1].TOCRE.BIT.TONEE0     = 0;   
  ATUE.SUBBLOCK[1].TIERE.BIT.CMEE0      = 0; //Timer Interrupt Enable Register
  ATUE.SUBBLOCK[1].RLDCRE.BIT.RLDENE0   = 1; //Reload Control Register
  ATUE.SUBBLOCK[1].TSRE.BIT.OVFE0       = 0; //Timer Status Register
  ATUE.SUBBLOCK[1].TSRE.BIT.CMFE0       = 0; //Timer Status Register
  ATUE.SUBBLOCK[1].PSCRE.BIT.PSCE       = 3; //Prescaler Register E, From H'0 to H'7 
  ATUE.SUBBLOCK[1].CRLDE[0]     = crankHWInitVal->pwmCycle; /* Cycle Register Value = (E Frequre/Cycle Set Value) */
  ATUE.SUBBLOCK[1].DRLDE[0]     = crankHWInitVal->pwmDuty;  /* Duty Register Value = (Cycle/100.00%)*Duty Set Value */ 
  ATUE.SUBBLOCK[1].TCNTE[0]             = 0; //Clear Timer Counter E
  ATUE.SUBBLOCK[1].SSTRE.BIT.SSTRE0     = 1; //Start  
   
  /* Set Timer A */
  ATUA.TCRA.BIT.EVOSEL1 = 1; //TIA00 is selected as event output1
  ATUA.TIOR1A.BIT.IOA0  = crankHWInitVal->edgeType; //TCNTA captured edge set of TIA00
  
  ATUA.TSRA.BIT.ICFA0   = 0; //Clear Input Capture Flag A0
  ATUA.TIERA.BIT.OVEA   = 0; //Disables an OVFA interrupt request
  ATUA.TIERA.BIT.ICEA0  = 0; //Disables a request of input capture interrupt A0
  
  /* Crank input noise filter Set */
  ATUCTRL.NCMR.BIT.NCMA = 1; //TIA Noise cancellation mode is Minimum time-at-level cancellation mode
  ATUA.TIOR2A.BIT.NCKA0 = 0;
  ATUA.TIOR2A.BIT.NCEA0 = 1; //Noise canceler for TIA00 are enabled
  ATUA.NCNTA0 = 0;           //Noise canceler counter A0 is initialized to 0
  ATUA.NCRA0  = crankHWInitVal->minPW * 1000 / 3200; //Init the noise canceler register A0, Set time / (P Clock / 128)
  
  /* Set Timer B */
  ATUB.TCRB.BIT.CKSELB  = 0; //Clock-bus line 0
  ATUB.TIORB.BIT.EVCNTB = 1; //Enables the externally input events
  ATUB.TSRB.BYTE        = 0; //Timer Status Register B is initialized to H'00
  
  
  /* Block for measuring edge intervals set of Timer B */
  ATUB.TIERB.BIT.CMEB1  = 1; //Output of compare match interrupt B1 is enabled
  ATUB.TIERB.BIT.ICEB0  = 0; //
  ATUB.TIERB.BIT.CMEB0  = 0;
  ATUB.TCNTB0           = 1;          //Edge interval measuring counter B0 is initialized to H'0000 0001
  ATUB.ICRB0            = 0;          //Input Capture register B0 is initialized to H'0000 0000
  ATUB.OCRB0            = 0xFFFFFFFF; //Output compare register B0 is initialized to H'FFFFFFFF
  ATUB.TCNTB1           = 0;          //Event Counter B1 is initialized to H'00
  ATUB.ICRB1            = 0;          //Input Capture register B1 is initialized to 
  ATUB.OCRB1            = OCRB1_VALUE; //Output compare register B1 is initialized to set value
  ATUB.ICRB2            = 0;          //Input Capture register B2 is initialized to H'0000 0000
  
  
  /* Block for generating frequency-multiplied clock signals set of Timer B */
  ATUB.TIORB.BIT.LDSEL  = 0;
  ATUB.TIORB.BIT.LDEN   = 0;  //TCNTB2 and RLDB are updated on ICEB0 input capture
  ATUB.TIORB.BIT.IOB6   = 1;  //Compare match between TCNTB6 and OCRB6 is enabled
  ATUB.TIERB.BIT.IREG   = 2; //An interrupt request is output when the second externally input event is detected after CMFB6 is enabled
  ATUB.TIERB.BIT.CMEB6  = 0; //Output of compare match interrupt B6 is disabled
  ATUB.LDB.BIT.LDVAL    = LOAD_REGISTER_B; //Load register is initialized to set value
  ATUB.RLDB.BIT.RLDVAL  = 0; //Reload register B is initialized to H'00000000
  ATUB.TCNTB2.BIT.CNTB2 = 0; //Reloadable counter B2 is initialized to H'00000000
  ATUB.PIMR.BIT.PIM     = PIMR_VALUE; //Pulse interval multiplier register is initialized to set value
  ATUB.TCNTB6.BIT.CNTB6 = 0; //Multiplied clock counter B6 is initialized to H'0000 0000
  ATUB.OCRB6.BIT.OCB6   = OCRB6_VALUE ;//Output compare register B6 is initialized to H'FFFFF000
  ATUB.OCRB7.BIT.OCB7   = OCRB7_VALUE ;//Output compare register B7 is initialized to H'FFFFF000
  
  
  /* Block for correcting frequency-multiplied clock set of Timer B */
  ATUB.TIORB.BIT.CTCNTB5 = 0;
  ATUB.TIORB.BIT.CCS     = 0; //TCNTB4 is in operation when TCNTB3 = TCNTB4
  ATUB.TCNTB3.BIT.CNTB3  = 0; //Correcting event counter B3 is initialized to H'00000000
  ATUB.TCNTB4.BIT.CNTB4  = 0; //Multiplied-and-corrected clock counter B4 is initialized to H'00000000
  ATUB.TCNTB5.BIT.CNTB5  = 1; //Multiplied-and-corrected clock generating counter B5 is initialized to H'00001000
  ATUB.TCCLRB.BIT.CCLRB  = TCCLRB_VALUE; //Correcting counter clearing register B is initialized to set value
  
  /* Register into interrupt vector table*/
  if (1 == ATUB.TIERB.BIT.CMEB1) //Output of compare match interrupt B1 is enabled
  {
    
  }
  
  /* Timer B enable */
  ATUCTRL.ATUENR.BIT.TBE = 1; //Timer B counter operation enabled
  
}

/**************************************************************************
 *Function   : MultiTooghMeasureISRHandler      
 *Description: OCRB1                                                  
 *Input      :                              
 *Output     :                              
 *Return     : 
 *G Variable :                              
 *Notes      :                                   
 **************************************************************************/
void MutiToothMeasureISRHandler( void )
{
  ATUB.TSRB.BIT.CMFB1 = 0; //Clear compare match flag B1
  ATUB.TCNTB1 = 0;         //Clear Event Counter B1
}

/**************************************************************************
 *Function   : SingleToothMeasureISRHandler      
 *Description: Crank input capture interrupt(TIA00)                                                  
 *Input      :                              
 *Output     :                              
 *Return     : 
 *G Variable :                              
 *Notes      :                                   
 **************************************************************************/
void SingleToothMeasureISRHandler( void )
{
  //A-DMAC
}

/**************************************************************************
 *Function   : SyncToothISRHandler      
 *Description:                                                   
 *Input      :                              
 *Output     :                              
 *Return     : 
 *G Variable :                              
 *Notes      :                                   
 **************************************************************************/
void SyncToothISRHandler( void )
{
  
}